<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="85px">
          <el-col :span="6">
            <el-form-item  label="客户名称">
              <el-select v-model="listQuery.fsiteNo" 
              v-if="this.$store.state.user.fsiteLevel==3"
              disabled clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <el-select v-model="listQuery.fsiteNo" 
              v-else
              clearable filterable class="filter-item" placeholder="请选择客户名称" @change="getFpointOptions(listQuery.fsiteNo)">
          <el-option
            v-for="item in fsiteNoBelongOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="监测点">
        <el-select v-model="listQuery.fpointNo" clearable filterable class="filter-item" placeholder="请选择监测点">
          <el-option v-for="item in fpointOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上级监测点">
        <el-select v-model="listQuery.fparentNo" clearable filterable class="filter-item" placeholder="请选择上级监测点">
          <el-option v-for="item in fpointOptions2" :key="item.fpointNo" :label="item.fpointName"
            :value="item.fpointNo" />
        </el-select>
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
        <el-button type="primary" @click="handleFilter">查找</el-button>
        <el-button type="success" @click="handleCreate">添加</el-button>
      </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <!-- 查询结果 -->
          <JNPF-table  v-loading="listLoading" :data="list" @row-click="rowclick">
            <el-table-column align="center" width="160" label="操作" class-name="small-padding">
              <template slot-scope="scope">
                <el-button type="text" @click="handleUpdate(scope.row)"> 编辑</el-button>
                <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                <el-dropdown trigger="click" @command="handleCommand">
                <span class="el-dropdown-link">
                  更多<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="a">预约</el-dropdown-item>
                  <el-dropdown-item command="a">物料</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              </template>
            </el-table-column>
            <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index" />
            <el-table-column :formatter="formatSite" align="left" label="客户名称" prop="fsiteNo" />
            <el-table-column align="left" label="监测点编号" prop="fpointNo" />
            <el-table-column align="left" label="监测点名称" prop="fpointName" />
            <el-table-column :formatter="formatfparentNo" align="left" label="上级监测点" prop="fparentNo" />
            <el-table-column align="left" label="监测点类型" prop="fpointFlag">
              <template slot-scope="scope">
                <span style="color:#247CAA">{{ scope.row.fpointFlag | siteTypeFilter }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" :width="buttonColor.state" label="状态" prop="fpointState">
              <template slot-scope="scope">
                <span :style="{ color: buttonColor.statedisable }" v-if="scope.row.fpointState === 0">{{ scope.row.fpointState |
                    statusFilter
                }}</span>
                <span :style="{ color: buttonColor.statestart }" v-else>{{ scope.row.fpointState | statusFilter }}</span>
              </template>
            </el-table-column>
          </JNPF-table>
          <paginations
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          />
      </div>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" top="10px" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="70%">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="100px" ref="dataForm" :rules="rules" :model="dataForm">
          <el-col :span="8">
             <el-form-item   label="客户名称" prop="fsiteNo">
              <el-select v-model="dataForm.fsiteNo" v-if="this.$store.state.user.fsiteLevel == 3" disabled clearable filterable placeholder="请选择">
                <el-option v-for="item in fsiteNoBelongOptions" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
              <el-select v-else v-model="dataForm.fsiteNo" :disabled="dialogStatus == 'update'" clearable filterable
                placeholder="请选择" @change="getStations(dataForm.fsiteNo, true)">
                <el-option v-for="item in fsiteNoBelongOptions" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="el-form-item-small " label="监测点类型" prop="fpointFlag">
              <el-select v-model="dataForm.fpointFlag" :disabled="dialogStatus == 'update'" class="filter-item"
                placeholder="请选择监测点类型">
                <el-option v-for="(key, value) in siteTypeMap" :key="key" :label="key" :value="value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="el-form-item-small " label="状态" prop="fpointState">
              <el-select v-model="dataForm.fpointState" class="filter-item" placeholder="请选择状态">
                <el-option v-for="(key, value) in statusMap" :key="key" :label="key" :value="value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item  label="监测点编号" prop="fpointNo">
              <el-input v-model="dataForm.fpointNo" :disabled="dialogStatus === 'update'" type="number" />
              <!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="监测点名称" prop="fpointName">
              <el-input v-model="dataForm.fpointName" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="负责人" prop="fperson">
              <el-select clearable filterable v-model="dataForm.fperson"
                  class="filter-item" placeholder="请选择负责人">
                  <el-option v-for="item in getListfperson" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
             <el-form-item  label="上级监测点" prop="fparentNo">
              <el-select @change="fparentNoChange()" v-model="dataForm.fparentNo" :disabled="dataForm.fpointFlag !== '0'"
                clearable class="filter-item" placeholder="请选择上级监测点">
                <el-option v-for="item in eqtActionOptions" :key="item.fpointNo" :label="item.fpointName"
                  :value="item.fpointNo" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item  label="楼层" prop="flayerName">
              <el-input v-model="dataForm.flayerName" >
                  <el-button slot="append" icon="el-icon-search" @click="searchDialoglayer"></el-button>
                </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="高度" prop="fpointH">
              <el-input v-model="dataForm.fpointH" @focus="pop3(dataForm.fsiteNo, dataForm.fparentNo)" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纬度" prop="fpointL">
              <el-input v-model="dataForm.fpointL" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度" prop="fpointD">
              <el-input v-model="dataForm.fpointD" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="宽度" prop="fpointW">
              <el-input v-model="dataForm.fpointW" @focus="pop3(dataForm.fsiteNo, dataForm.fparentNo)" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item class="text" label="视频URL" prop="fvideoUrl">
              <el-input v-model="dataForm.fvideoUrl" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="实验室图片(2M内)" prop="fimage">
              <!--<el-input v-model="dataForm.fimage"/>-->
              <el-upload v-model="dataForm.fimage" :show-file-list="false" :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload" :action="uploadPath" :data="formdata2(dataForm.fpointNo)"
                class="avatar-uploader">
                <img v-if="dataForm.fimage" :src="dataForm.fimage" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="info"  @click="cancel()">取消
        </el-button>
        <el-button type="primary" v-if="dialogStatus == 'create'" 
          @click="createData">确定</el-button>
        <el-button  v-else type="primary"  @click="updateData">确定
        </el-button>
      </span>
    </el-dialog>
    <!-- 物料添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" :tlte="textMap[dialogStatus]" :visible.sync="dialogFormVisibleMaterial" width="80%">
      <el-tabs  type="border-card">
          <el-tab-pane label="头表" style="height:400px">
            <el-row class="JNPF-common-search-box" :gutter="16">
              <el-form @submit.native.prevent label-width="100px" ref="dataFormMaterial" :rules="rules" :model="dataForm">
                <el-col :span="8">
                  <el-form-item label="客户名称" prop="fsiteNo">
              <el-select v-model="dataForm.fsiteNo" disabled clearable filterable>
                <el-option v-for="item in fsiteNoBelongOptions" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="监测点编号" prop="fpointNo">
              <el-input v-model="dataForm.fpointNo" disabled type="number" />
            </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="监测点名称" prop="fpointName">
              <el-input disabled v-model="dataForm.fpointName" />
            </el-form-item>
                </el-col>
              </el-form>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="物料列表">
            <JNPF-table height="400px" v-loading="listLoading" :data="dataForm.details" stripe
                >
                <el-table-column :index="indexMethod" align="center" label="序" type="index" />
                <el-table-column align="left" label="物料编号" prop="fgoodno">
                   <template slot-scope="scope">
                  <el-input 
                    v-model="scope.row.fgoodno" 
                    v-direction="{ x: 0, y: scope.$index,type:3}"
                  >
                     <el-button slot="append" icon="el-icon-search" @click="handleUpdate1"></el-button>
                  </el-input>
                </template>
                </el-table-column>
                <el-table-column :formatter="formatfgoodno" align="left" label="物料名称" prop="fgoodname" />
                <el-table-column align="left" label="安全库存" prop="fsafeqty" />
                <el-table-column :width="buttonColor.operationTwo" align="center" label="操作" class-name="small-padding">
                  <template slot-scope="scope">
                  <el-button type="danger" @click="handleDelete1(scope.row)">删除</el-button>
                  </template>
                </el-table-column>
              </JNPF-table>
          </el-tab-pane>
        </el-tabs>
    </el-dialog>
    <!-- 预约 -->
    <el-dialog width="50%" top="10px" :title="textMap[dialogStatus]" :close-on-click-modal="false" :visible.sync="dialogFormVisibleOrder">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="85px" label-position="left" ref="dataFormOrder" :model="dataForm">
          <el-col :span="12">
            <el-form-item  label="客户名称" prop="fsiteNo">
              <el-select v-model="dataForm.fsiteNo" disabled clearable filterable>
                <el-option v-for="item in fsiteNoBelongOptions" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="监测点编号" prop="fpointNo">
              <el-input v-model="dataForm.fpointNo" disabled type="number" />
              <!-- oninput="value=value.replace(/[^\d]/g,'')"-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测点名称" prop="fpointName">
              <el-input disabled v-model="dataForm.fpointName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="面积(km²)" prop="fsize">
              <el-input v-model="dataForm.fsize" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收费方式" prop="fcharge">
              <el-select v-model="dataForm.fcharge" clearable filterable>
                <el-option v-for="item in fchargeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收费标准" prop="fchargeprice">
              <el-input v-model="dataForm.fchargeprice" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议人数" prop="fcustnum">
              <el-input v-model.number="dataForm.fcustnum" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台编号" prop="fappno">
              <el-input v-model="dataForm.fappno" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="门禁类型" prop="fmjtype">
              <el-select v-model="dataForm.fmjtype" clearable filterable>
                <el-option v-for="item in fmjtypeOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="info"
          @click="dialogFormVisibleOrder = false">取消</el-button>
        <el-button type="primary" @click="updateDataOrder">确定
        </el-button>
      </span>
    </el-dialog>
     <!-- 搜索弹出框 -->
    <userDialog :searchVisibleuse.sync="searchVisibleuse" :fsiteNo.sync="userDialogFsiteNo" :tableColor="tableColor"
      @userDialog="userDialogValue" />
    <layerDialog :layerVisible.sync="layerVisible" :fparentNo.sync="layerDialogfparentNo"
      :fsiteNo.sync="layerDialogFsiteNo" :tableColor="tableColor" @layerDialog="layerDialogValue" />
  </div>
</template>

<script>
import $ from 'jquery'
import { listRoom, createRoom, updateRoom, deleteRoom,uploadPath } from '@/api/room'
import { getToken } from '@/utils/auth'
import { getApiUrl } from '@/utils/request'
import { fsiteOptionsByLevel, getListArea, uploadPath2 } from '@/api/area'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import cmap from './baiduMap'
import { listPointByFsiteNo2 } from '@/api/airAlarm'
import { tableColor, titleColor, buttonColor, exportColor } from '@/filters/index'
import { listtsLayer } from '@/api/tsLayer'
import LayerDialog from '@/components/LayerDialog'
import UserDialog from '@/components/UserDialog'
import Material from '@/components/Material'

const siteTypeMap = {
  0: '室内'
}

const statusMap = {
  0: '禁用',
  1: '启用'
}

export default {
  name: 'Lab',
  components: { Pagination, cmap, LayerDialog, UserDialog, Material },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type]
    },
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      fmjtypeOptions: [], // 门禁类型
      fchargeOptions: [], // 收费方式
      goodAllList: [],
      selectList: [],
      materialIdVisible: false,
      materialFsiteNo: null,
      isAdd1: true,
      panelHeight: 0,
      uploadVisible: false,
      downUrl: null,
      exportColor,
      getListfperson: [],
      layerListAll: [],
      searchVisibleuse: false,
      userDialogFsiteNo: null,
      userFlag: null,
      layerVisible: false,
      layerDialogFsiteNo: null,
      layerDialogfparentNo: null,
      uploadPath,
      uploadPath2,
      list: null,
      total: 0,
      parentnoAll: [],
      fsiteNoBelongOptions: [],
      fpointOptions: null,
      fpointOptions2: null,
      imgUrl: null,
      center: '北京',
      lat: 0,
      lng: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fsiteNo: undefined,
        fpointNo: undefined,
        fimage: undefined,
        fparentNo: undefined,
        fpointFlag: undefined,
        fpointState: 2,
        sort: 'create_time',
        order: 'desc'
      },
      siteTypeMap,
      statusMap,
      dataForm: {
        id: undefined,
        fpointNo: undefined,
        fimage: undefined,
        fpointName: undefined,
        fsiteNo: undefined,
        fparentNo: undefined,
        fpointFlag: undefined,  // 站点类型(0:教室,1:室外,2:教学楼)
        fpointL: undefined,
        fpointD: undefined,
        fpointH: undefined,
        fpointW: undefined,
        fvideoUrl: undefined,
        fpointState: undefined,
        fupddate: undefined,
        foperator: undefined,
        fperson: undefined,
        flayerNo: undefined,
        flayerName: undefined,
        fcustnum: undefined,
        fcharge: undefined,
        fchargeprice: undefined,
        fsize: undefined,
        fappno: undefined,
        fmjtype: undefined,
        details: []
      },
      detailsTmp: [],
      listTmp: [],
      dataForm1: {
        fsiteNo: undefined,
        fgoodno: undefined,
        fgoodname: undefined,
        fsafeqty: undefined,
        id: undefined
      },
      eqtActionOptions: [],
      dialogFormVisibleOrder: false, // 预约
      dialogFormVisibleMaterial: false, // 物料
      dialogFormVisible1: false, // 物料子表1
      dialogFormVisible: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rowdata:{},
      tableColor: tableColor,
      titleColor: titleColor,
      buttonColor: buttonColor,
      rules: {
        fpointNo: [
          { required: true, message: '监测点编号不能为空', trigger: 'blur' }
        ],
        fpointName: [
          { required: true, message: '监测点名称不能为空', trigger: 'blur' }
        ],
        fsiteNo: [
          { required: true, message: '客户名称不能为空', trigger: 'blur' }
        ],
        fparentNo: [
          { required: true, message: '上级监测点编号不能为空', trigger: 'blur' }
        ],
        flayerName: [
          { required: true, message: '楼层不能为空', trigger: 'blur' }
        ],
        fpointFlag: [
          { required: true, message: '监测点类型不能为空', trigger: 'blur' }
        ],
        fperson: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        fpointState: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      rules1: {
        fgoodno: [
          { required: true, message: '物料编号不能为空' },
        ],
        fsafeqty: [
          { required: true, message: '安全库存不能为空' },
          { type: 'number', message: '安全库存必须为数字值' },
        ],
      },
    }
  },
  computed: {
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.keyCode();
    this.downUrl = getApiUrl() + 'xlsx/1-8实验室.xlsx'
    this.panelHeight = document.documentElement.clientHeight - 378
    if (this.$store.state.user.fsiteLevel == 3) {
      this.listQuery.fsiteNo = this.$store.state.user.fsiteNo
      getListArea({ flag: 10, arg1: this.$store.state.user.fsiteNo })
        .then(response => {
          this.fpointOptions = response.data.data.list
        })
        .catch(() => {
          this.fpointOptions = []
        })
      listPointByFsiteNo2(this.$store.state.user.fsiteNo)
        .then(response => {
          this.fpointOptions2 = response.data.data.list
        })
        .catch(() => {
          this.fpointOptions2 = []
        })
      getListArea({ flag: 13, arg1: this.$store.state.user.fsiteNo })
        .then(response => {
          this.getListfperson = response.data.data.list
        })
        .catch(() => {
          this.getListfperson = []
        })
      this.fchargeOptions = [
        {
          value: 1, label: '小时'
        },
        {
          value: 2, label: '次数'
        },
      ]
    }

    this.getList()
    fsiteOptionsByLevel(3)
      .then(response => {
        this.fsiteNoBelongOptions = response.data.data.list
      })
      .catch(() => {
        this.fsiteNoBelongOptions = []
      })

    listPointByFsiteNo2()
      .then(response => {
        this.parentnoAll = response.data.data.list
      })
      .catch(() => {
        this.parentnoAll = []
      })

    // 物料
    getListArea({ flag: 31 })
      .then(response => {
        this.goodAllList = response.data.data.list
      })
      .catch(() => {
        this.goodAllList = []
      })

      // 门禁类型
    getListArea({ flag:34,arg1:2020 })
      .then(response => {
        this.fmjtypeOptions = response.data.data.list
      })
      .catch(() => {
        this.fmjtypeOptions = []
      })

  },
  methods: {
    handleCommand(e){
      if(e=="a")
      {
        this.order(this.rowdata);
      }
      else{
        this.material(this.rowdata);
      }
    },
    rowclick(data){
      this.rowdata=data;
    },
     //回车
    keyCode() {
      let direction = this.$getDirection();
      let _this=this;
      direction.on("keyup", function (e, val) {
        if (e.keyCode === 13) {
          if(val.y==_this.dataForm.details.length-1)
          {
            _this.addrow(res=>{
               direction.next();
            })
          }
          else
          {
           direction.next();
          }
        }
        if (e.keyCode === 37) {
          direction.previous();
        }
        if (e.keyCode === 38) {
          direction.previousLine();
        }
        if (e.keyCode === 40) {
          direction.nextLine();
        }
        if (e.keyCode == 39) {
          direction.next();
        }
      });
    },
    //添加行
    addrow(callback){
      this.dataForm.details.push({
        fseq:this.dataForm.details.length+1,
         fgoodno:"",
        formatfgoodno:"",
        fsafeqty:""
      })
      setTimeout(()=>{
      callback(200)
      },300)
    },
    // 物料弹出框
    searchDialogMaterial() {
      this.materialIdVisible = true
      this.materialFsiteNo = this.dataForm.fsiteNo
    },

    // 物料弹出框
    materialDialogValue(value) {
      this.dataForm1.fgoodno = value.data.fgoodno
      this.dataForm1.fgoodname = value.data.fgoodname
      this.dataForm1.fsafeqty = value.data.fsafeqty
    },
    // 上级监测点
    fparentNoChange() {
      this.dataForm.flayerNo = ''
      this.dataForm.flayerName = ''
    },

    // 搜索人员弹出框
    searchDialoguse(value) {
      this.userFlag = value
      if (value === 'add') {
        this.userDialogFsiteNo = this.dataForm.fsiteNo
      } else {
        this.userDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisibleuse = true
    },

    // 人员弹出框返回值
    userDialogValue(value) {
      if (this.userFlag === 'add') {
        console.log(value.user)
        this.dataForm.fperson = value.user.id.toString()
      } else {
        this.listQuery.fperson = value.user.id
      }
    },

    // 楼层弹出框
    searchDialoglayer(value) {
      this.layerDialogFsiteNo = this.dataForm.fsiteNo
      if (this.dataForm.fparentNo) {
        this.layerDialogfparentNo = this.dataForm.fparentNo
      } else {
        this.$notify.info({
          message: '请先选择上级监测点'
        })
        return
      }
      this.layerVisible = true
    },

    // 楼层弹出框返回值
    layerDialogValue(value) {
      this.dataForm.flayerName = value.data.flayerName
      this.dataForm.flayerNo = value.data.flayerNo
    },

    /** *
     * 图片点击事件。
     */
    getClickInfo(e) {
      document.getElementById('span').style.setProperty('left', (e.offsetX - 20).toString() + 'px')
      document.getElementById('span').style.setProperty('top', (e.offsetY - 25).toString() + 'px')
    },
    setPosition() {
      this.dialogFormVisible3 = false
      var a = document.getElementById('image')
      this.dataForm.fpointH = a.clientHeight
      this.dataForm.fpointW = a.clientWidth
      this.dataForm.fpointD = document.getElementById('span').style.getPropertyValue('left').replace('px', '')
      this.dataForm.fpointL = document.getElementById('span').style.getPropertyValue('top').replace('px', '')
      this.$refs['dataForm'].validateField('fpointL')
      this.$refs['dataForm'].validateField('fpointD')
      this.$refs['dataForm'].validateField('fpointH')
      this.$refs['dataForm'].validateField('fpointW')
    },
    pop3(fsiteNo, fparentNo) {
      if ((fparentNo === undefined || fparentNo === null || fparentNo === '' || fparentNo === '0') && (fsiteNo === undefined || fsiteNo === null || fsiteNo === '')) {
        this.$notify.info({
          message: '请先选择 客户名称 或 上级监测点'
        })
        return
      }
      if (fparentNo === undefined || fparentNo === null || fparentNo === '' || fparentNo === '0') { // 教学楼
        // this.imgUrl = 'air/images/002/site/' + fsiteNo + '.png'
        this.imgUrl = '300003.png'
      } else { // 教室
        this.imgUrl = 'air/images/002/indoor/' + fparentNo + '.png'
      }
      var that = this
      $.ajax({
        type: 'get',
        // url: 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png',
        // url: this.imgUrl,
        url: '300003.png',
        success: function (data) {
          that.dialogFormVisible3 = true
        },
        error: function (data) {
          return
        }
      })
    },
    // 导入
    uploadFileSuccess: function (response) {
      this.uploadVisible = false
      this.$notify.success({
        title: '导入',
        dangerouslyUseHTMLString: true,
        message: response.data
      })
    },
    handleUpload() {
      this.uploadVisible = true
    },
    formdata2: function (fpointno) {
      return { 'ftype': '1', 'fpointno': fpointno }
    },
    handleAvatarSuccess(res, file) {
      // this.dataForm.fimage = URL.createObjectURL(file.raw)
      this.dataForm.fimage = res.data
      const time = new Date().getTime()
      this.dataForm.fimage = `${this.dataForm.fimage}?time=${time}`
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isLt2M // isJPG &&
    },
    formatStatus: function (row, column) {
      return row.fpointState === 1 ? '启用' : '禁用'
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    formatSite: function (row, column) {
      if (this.fsiteNoBelongOptions.length > 0) {
        for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
          if (row.fsiteNo === this.fsiteNoBelongOptions[i].value) {
            return this.fsiteNoBelongOptions[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 客户名称
    formatfparentNo(row) {
      if (this.parentnoAll.length > 0) {
        for (let i = 0; i < this.parentnoAll.length; i++) {
          if (row.fparentNo == this.parentnoAll[i].fpointNo) {
            return this.parentnoAll[i].fpointName
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 物料名称
    formatfgoodno: function (row, column) {
      if (this.goodAllList.length > 0) {
        for (let i = 0; i < this.goodAllList.length; i++) {
          if (row.fgoodno === this.goodAllList[i].value) {
            return this.goodAllList[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 客户名称改变
    getFpointOptions(fsiteNo) {
      this.listQuery.fpointNo = undefined
      this.listQuery.fparentNo = undefined
      this.fpointOptions = []
      this.fpointOptions2 = []
      if (fsiteNo) {
        getListArea({ flag: 10, arg1: fsiteNo })
          .then(response => {
            this.fpointOptions = response.data.data.list
          })
          .catch(() => {
            this.fpointOptions = []
          })
        listPointByFsiteNo2(fsiteNo)
          .then(response => {
            this.fpointOptions2 = response.data.data.list
          })
          .catch(() => {
            this.fpointOptions2 = []
          })
      }
    },
    // 获取客户名称
    getStations(fsiteNo, clearFparentNo) {
      this.eqtActionOptions = []
      if (clearFparentNo) {
        this.dataForm.fparentNo = ''
        this.dataForm.flayerNo = ''
        this.dataForm.flayerName = ''
      }
      if (fsiteNo) {
        listPointByFsiteNo2(fsiteNo)
          .then(response => {
            this.eqtActionOptions = response.data.data.list
          })
          .catch(() => {
            this.eqtActionOptions = []
          })
        getListArea({ flag: 13, arg1: fsiteNo })
          .then(response => {
            this.getListfperson = response.data.data.list
          })
          .catch(() => {
            this.getListfperson = []
          })
      }
    },
    getList() {
      this.listLoading = true
      this.listQuery.fpointFlag = 0
      listRoom(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fpointNo: undefined,
        fimage: undefined,
        fpointName: undefined,
        fsiteNo: undefined,
        fparentNo: undefined,
        fpointFlag: undefined,
        fpointL: undefined,
        fpointD: undefined,
        fpointH: undefined,
        fpointW: undefined,
        fvideoUrl: undefined,
        fpointState: undefined,
        fupddate: undefined,
        foperator: undefined,
        fperson: undefined,
        flayerNo: undefined,
        flayerName: undefined,
        details: []
      }
    },
    resetForm1() {
      this.dataForm1 = {
        fgoodno: undefined,
        fgoodname: undefined,
        fsafeqty: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.fpointState = '1'
      this.dataForm.fpointFlag = '0'
      this.eqtActionOptions = []
      if (this.$store.state.user.fsiteLevel == 3) {
        this.dataForm.fsiteNo = this.$store.state.user.fsiteNo
        listPointByFsiteNo2(this.$store.state.user.fsiteNo)
          .then(response => {
            this.eqtActionOptions = response.data.data.list
          })
          .catch(() => {
            this.eqtActionOptions = []
          })
      }
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      this.resetForm1();
      this.isAdd1 = true
    },
    clickMap() {
      // this.resetForm()
      this.dialogFormVisible2 = true
      this.lat = 0
      this.lng = 0
      // this.dataForm.fsiteState = '1'
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    getPoint(point) {
      this.lat = point.lat
      this.lng = point.lng
    },
    setLatLng() {
      this.dialogFormVisible2 = false
      this.dataForm.fpointL = this.lat
      this.dataForm.fpointD = this.lng
      this.$refs['dataForm'].validateField('fpointL')
      this.$refs['dataForm'].validateField('fpointD')
    },
    // 楼层名称
    formatflayerName(row) {
      if (this.layerListAll.length > 0) {
        for (let i = 0; i < this.layerListAll.length; i++) {
          if (row.flayerNo == this.layerListAll[i].flayerNo) {
            return this.layerListAll[i].flayerName
          }
        }
      } else {
        return ''
      }
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id
          this.dataForm.fupddate = new Date()
          createRoom(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加成功'
              })
              this.selectList = []
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dataForm.fpointFlag = row.fpointFlag.toString()
      this.dataForm.fpointState = row.fpointState.toString()
      this.getStations(row.fsiteNo, false)

      listtsLayer({ limit: -1, fsiteNo: row.fsiteNo, fpointNo: row.fparentNo })
        .then(response => {
          this.layerListAll = response.data.data.list
          for (let i = 0; i < this.layerListAll.length; i++) {
            if (row.flayerNo == this.layerListAll[i].flayerNo) {
              this.dataForm.flayerName = this.layerListAll[i].flayerName
            }
          }
          this.dialogStatus = 'update'
          this.dialogFormVisible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
          })
        })
        .catch(() => {
          this.layerListAll = []
        })

      this.listTmp = row;
      this.detailsTmp = [];
      row.details.forEach((v) => {
        this.detailsTmp.push(v);
        this.selectList.push(v.fgoodno)
      });
      this.resetForm1();
      this.isAdd1 = true
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id
          this.dataForm.fupddate = new Date()
          updateRoom(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fpointNo === this.dataForm.fpointNo) {
                  const index = this.list.indexOf(v)
                  this.dataForm.fpointState = parseInt(this.dataForm.fpointState)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新成功'
              })
              this.selectList = []
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否删除该资料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRoom(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.data
            })
          })
      })
    },
    // 预约
    order(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogFormVisibleOrder = true
      if(this.dataForm.fmjtype){
        this.dataForm.fmjtype = this.dataForm.fmjtype.toString()
      }
      this.dialogStatus = 'update'
      this.$nextTick(() => {
        this.$refs['dataFormOrder'].clearValidate()
      })
    },
    updateDataOrder() {
      this.$refs['dataFormOrder'].validate(valid => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id
          this.dataForm.fupddate = new Date()
          updateRoom(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fpointNo === this.dataForm.fpointNo) {
                  const index = this.list.indexOf(v)
                  this.dataForm.fpointState = parseInt(this.dataForm.fpointState)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisibleOrder = false
              this.$notify.success({
                title: '成功',
                message: '更新预约成功'
              })
              this.selectList = []
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    // 物料窗口打开
    material(row) {
      this.dataForm = Object.assign({}, row)
      this.dataForm.details.push({
        fgoodno:"",
        formatfgoodno:"",
        fsafeqty:""
      })
      this.dialogStatus = 'update'
      this.listTmp = row;
      this.detailsTmp = [];
      row.details.forEach((v) => {
        this.detailsTmp.push(v);
        this.selectList.push(v.fgoodno)
      });
      this.resetForm1();
      this.isAdd1 = true
      console.log('this.dataForm ', this.dataForm);
      this.dataForm.fpointFlag = row.fpointFlag.toString()
      this.dialogFormVisibleMaterial = true

    },
    // 物料确定
    updateDataMaterial() {
      this.$refs['dataFormMaterial'].validate(valid => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id
          this.dataForm.fupddate = new Date()
          updateRoom(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fpointNo === this.dataForm.fpointNo) {
                  const index = this.list.indexOf(v)
                  this.dataForm.fpointState = parseInt(this.dataForm.fpointState)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisibleMaterial = false
              this.$notify.success({
                title: '成功',
                message: '更新物料成功'
              })
              this.selectList = []
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.data
              })
            })
        }
      })
    },
    // 物料取消
    cancleMaterial() {
      this.dialogFormVisibleMaterial = false
      this.selectList = []
      this.list.forEach((v) => {
        if (v.fpointNo === this.dataForm.fpointNo) {
          this.dataForm = this.listTmp
          this.dataForm.details = this.detailsTmp
          const index = this.list.indexOf(v)
          this.list.splice(index, 1, this.dataForm)
          return
        }
      })
    },
    cancel() {
      this.dialogFormVisible = false
      this.selectList = []
      this.list.forEach((v) => {
        if (v.fpointNo === this.dataForm.fpointNo) {
          this.dataForm = this.listTmp
          this.dataForm.details = this.detailsTmp
          const index = this.list.indexOf(v)
          this.list.splice(index, 1, this.dataForm)
          return
        }
      })
    },
    // 弹窗点击关闭前的回调
    handleClose() {
      this.dialogFormVisible = false
      this.selectList = []
      this.list.forEach((v) => {
        if (v.fpointNo === this.dataForm.fpointNo) {
          this.dataForm = this.listTmp
          this.dataForm.details = this.detailsTmp
          const index = this.list.indexOf(v)
          this.list.splice(index, 1, this.dataForm)
          return
        }
      })
    },
    handleUpdate1() {
      this.resetForm1()
      this.dataForm1.fsiteNo = this.$store.state.user.fsiteNo
      this.dialogFormVisible1 = true
      // 清除验证
      this.$nextTick(() => {
        this.$refs["dataForm1"].clearValidate();
      });
    },

    // 子表一添加编辑
    handleEdit1(row) {
      this.dialogFormVisible1 = true
      this.dataForm1 = Object.assign({}, row);
      this.dataForm1.fgoodname = this.formatfgoodno(row)
      // 清除验证
      this.$nextTick(() => {
        this.$refs["dataForm1"].clearValidate();
      });
      this.isAdd1 = false
    },
    // 子表一编辑保存数据
    editData1() {
      this.$refs["dataForm1"].validate((valid) => {
        if (valid) {
          if (this.dataForm.details.length > 0) {

            // 子表添加
            if (this.isAdd1) {
              // 判断物料id是否重复
              const tem = this.selectList.indexOf(this.dataForm1.fgoodno)
              if (tem < 0) {
                if (valid) {
                  this.dataForm.details.push(this.dataForm1)
                  this.selectList.push(this.dataForm1.fgoodno)
                }
              } else {
                this.$notify.error({
                  title: '失败',
                  message: '所选物料重复'
                })
                return;
              }
            } else {
              // 子表编辑
              for (const v of this.dataForm.details) {
                if (v.fgoodno === this.dataForm1.fgoodno) {
                  const index = this.dataForm.details.indexOf(v);
                  this.dataForm.details.splice(index, 1, this.dataForm1);
                  break;
                }
              }
            }
          } else {
            // 判断物料id是否重复
            const tem = this.selectList.indexOf(this.dataForm1.fgoodno)
            if (tem < 0) {
              if (valid) {
                this.dataForm.details.push(this.dataForm1)
                this.selectList.push(this.dataForm1.fgoodno)
              }
            } else {
              this.$notify.error({
                title: '失败',
                message: '所选物料重复'
              })
              return;
            }
          }
          this.resetForm1()
          this.isAdd1 = true
          // 清除验证
          this.$nextTick(() => {
            this.$refs["dataForm1"].clearValidate();
          });
          this.dialogFormVisible1 = false
        }
      });
    },

    cancle1() {
      this.resetForm1()
      this.isAdd1 = true

      // 清除验证
      this.$nextTick(() => {
        this.$refs["dataForm1"].clearValidate();
      });
      this.dialogFormVisible1 = false
    },

    handleDelete1(row) {
      this.$confirm('是否要删除物料', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.dataForm.details.indexOf(row);
        this.dataForm.details.splice(index, 1);
        this.selectList.splice(index, 1)
        this.resetForm1()
        this.isAdd1 = true
      }).catch(() => { })

    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
text-align: center;
}
</style>